﻿using System;
using System.Collections.Generic;
using MdcService.Structure;
using MdcService.Structure.Enum;

namespace MdcService.Machine
{
	public abstract class BaseMachine : IDisposable
	{
		public virtual string Name { get;  set; }
		public virtual string Id { get;  set; }
		public virtual string Path { get;  set; }
		public virtual MachineType Type { get; set; }
		public virtual bool IsConnected { get; set; }
		public virtual DateTime LastConnected { get; set; }
		public virtual DateTime LastRead { get; set; }
		private int _failures = 0;
		public virtual int Failures
		{
			get { return _failures; }
			set { _failures = _failures > 1000 ? 1000 : value; }
		}

		public virtual Dictionary<string,ValueItem> Values { get;  set; } = new Dictionary<string, ValueItem>();

		public abstract void Connect();
		public abstract void Disconnect();
		public abstract void Read();

	    internal void RefreshItem(ValueItem item)
	    {
	        Values[item.Path] = item;
	    }

	    internal void RefreshItem(string path, string value)
	    {
	        Values[path].Value = value;
	    }

		private void TagRefresh(string path, string value)
		{
			Values[path].Value = value;
		}

		private void Add(string path,string description)
		{
			Values.Add(path,new ValueItem
			{
				Path = path,
				Description = description,
				Value = null,
			});
		}

		public virtual void Dispose()
		{
			this.Disconnect();
			this.Values = null;
		}
	}
}
